package com.examples.ry.layouts;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class WrapControlFormLayout {

	public static void main(String[] args) {
		Display display = new Display();
		final Shell shell = new Shell(display);
		int barStyle = SWT.FLAT | SWT.WRAP;
		final ToolBar toolBar = new ToolBar(shell, barStyle);
		for (int i = 0; i < 12; i++) {
			ToolItem item = new ToolItem(toolBar, SWT.PUSH);
			item.setText("Item " + i);
		}
		int style = SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL;
		Text text = new Text(shell, style);
		FormLayout layout = new FormLayout();
		shell.setLayout(layout);
		final FormData toolData = new FormData();
		toolData.top = new FormAttachment(0);
		toolData.left = new FormAttachment(0);
		toolData.right = new FormAttachment(100);
		toolBar.setLayoutData(toolData);
		FormData textData = new FormData();
		textData.top = new FormAttachment(toolBar);
		textData.bottom = new FormAttachment(100);
		textData.left = new FormAttachment(0);
		textData.right = new FormAttachment(100);
		text.setLayoutData(textData);
		shell.addListener(SWT.Resize, new Listener() {
			public void handleEvent(Event e) {
				Rectangle rect = shell.getClientArea();
				toolData.width = rect.width;
			}
		});
		text.setFocus();
		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

}
